<?php
function getChildren($cates,$parentID){
	$items = array();
	foreach($cates as $cate){
		if($cate['parentID'] == $parentID){
			$items[] = $cate;
		}
	}
	return $items;
}
function cateTr($cates,$cate,$currentID){
	$str = "<tr class=\"odd\"><td width=\"100\">".$cate['id']."</td>";
	$str .= "<td style='text-align:left;' width=\"200\">".str_repeat('&ndash;',substr_count($cate['path'],',')-1).$cate['title']."</td>";
	$str .= "<td width=\"400\">".$cate['description']."</td>";
	$str .= "<td width=\"100\"><a class=\"ajax2\" href='".url('category/edit?id='.$cate['id'])."' >编辑</a>&nbsp;&nbsp;|&nbsp;&nbsp;";
	$str .= "<a class='del' id='".$cate['id']."' href='javascript:void(0);' >删除</a></td>";
	echo $str."</tr>";
	$children = getChildren($cates,$cate['id']);
	if(!empty($children)){
		foreach($children as $child){
			cateTr($cates,$child,$currentID);
		}
	}
}
$currentID = isset($currentID)?$currentID:0;
?>
<div class="list">
<span class="txt"><a class="ajax" href="<?php echo url('category/add?type='.$type);?>"></a></span>
	<table cellspacing="0" cellpadding="0">
		<tr class="th">
			<th>ID</th>	
			<th>名称</th>		
			<th>描述</th>	
			<th>操作</th>	
		</tr>
		<?php foreach($topCate as $cate):?>
		<?php cateTr($cates,$cate,$currentID);?>
		<?php endforeach;?>

	</table>
</div>
<script type="text/javascript">
$(document).ready(function(){
	$(".odd:odd").css("background-color","#FFFAF0");
	$(".ajax").mouseover(function(){
        url = $(this).attr('href');
        $(".ajax").wBox({requestType: "ajax",title:"分类添加",target:url});
    });
	$(".ajax2").mouseover(function(){
		url = $(this).attr('href');
        $(".ajax2").wBox({requestType: "ajax",title:"分类修改",target:url});
    });
	$(".del").click(function(){
		if(confirm("确认删除")){
			that = $(this);
			$.post("<?php echo url('category/del');?>",{id:that.attr('id')},function(data){
				if(data.s==1){
					that.parents('tr').remove();
				}else{
					alert(data.i);
				}
			});
		}
		return false;
		});	
});
</script>
